package cn.cvs.service.user;

import cn.cvs.dao.SysUserMapper;
import cn.cvs.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService{
    @Resource
    private SysUserMapper sysUserMapper;
    /**
     * 验证用户名和密码是否正确
     *
     * @param account
     * @param passWord
     * @return SysUser
     * @throws Exception
     */
    @Override
    public SysUser checkUser(String account, String passWord) throws Exception {
        //根据账号获取用户对象
        SysUser sysUser = sysUserMapper.queryUserByAccount(account);
        if (sysUser!=null){
            //存在该账号，进行密码对比
            if (passWord.equals(sysUser.getPassword())){
                return sysUser;
            }

        }
        return null;
    }

    /**
     * 分页查询
     *
     * @param pageIndex 页码
     * @param pageSize  每页显示的记录数woow
     * @return
     * @throws Exception
     */
    @Override
    public List<SysUser> findUserByPage(Integer pageIndex, Integer pageSize,String queryRealName,Integer queryRoleId) throws Exception {
        //根据页码获取偏移量（pageIndex-1）*pageSize
        Integer from = (pageIndex-1)*pageSize;
        List<SysUser> list = sysUserMapper.queryUserListByPage(from, pageSize,queryRealName,queryRoleId);
        return list;
    }

    /**
     * 根据ID获取用户对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public SysUser findUserById(String id) throws Exception {
        return sysUserMapper.queryUserById(id);
    }

    /**
     * 获取用户总记录
     *
     * @return
     * @throws Exception
     */
    @Override
    public Integer findUserTotalCount(String queryRealName,Integer queryRoleId) throws Exception {
        return sysUserMapper.queryUserTotalCount(queryRealName,queryRoleId);
    }


    /**
     * 添加用户
     *
     * @param sysUser
     * @return
     * @throws Exception
     */
    @Override
    public boolean saveSysUser(SysUser sysUser) throws Exception {
        Integer row = sysUserMapper.addUser(sysUser);
        if (row>0){
            return true;
        }else {
            return false;
        }

    }

    /**
     * 更新用户信息
     *
     * @param sysUser
     * @return
     * @throws Exception
     */
    @Override
    public boolean updateUser(SysUser sysUser) throws Exception {
        Integer row = sysUserMapper.modifyUser(sysUser);
        if (row>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 通过账号获取用户对象
     *
     * @param account
     * @return
     * @throws Exception
     */
    @Override
    public SysUser findUserByAccount(String account) throws Exception{
        return sysUserMapper.queryUserByAccount(account);
    }

    /**
     * 根据用户id修改密码
     *
     * @param id
     * @param newPWD
     * @return
     * @throws Exception
     */
    @Override
    public boolean updatePWD(String id, String newPWD) throws Exception {
        Integer row = sysUserMapper.modifyPWD(id, newPWD);
        if (row>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 根据Id删除用户
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public boolean delUserById(String id) throws Exception {
        Integer row = sysUserMapper.deleteUserById(id);
        if (row>0){
            return true;
        }else {
            return false;
        }
    }
}
